package com.gardenguell.salad.util;

import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.DhcpInfo;
import android.net.NetworkInfo;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Message;
import android.provider.Settings;
import com.gardenguell.salad.app.AppContext;
import com.gardenguell.salad.app.HandlerCenter;

/**
 * Created by absurd on 2015/11/19.
 */
public class WifiUtil {


    private WifiManager mWifiManager;

    private static WifiUtil mWifiUtil;

    private WifiUtil(Context context){
        this.mWifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
    }

    public void connectWifi(String ssid, String password){
        WifiConfiguration wifiConfig = new WifiConfiguration();
        wifiConfig.SSID = String.format("\"%s\"", ssid);
        wifiConfig.preSharedKey = String.format("\"%s\"", password);

        int netId = mWifiManager.addNetwork(wifiConfig);
        mWifiManager.disconnect();
        mWifiManager.enableNetwork(netId, true);
    }

    public boolean isWifiContected(String ssid){
        ConnectivityManager connectivityManager
                = (ConnectivityManager)AppContext.getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo wifiNetworkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if(wifiNetworkInfo.isConnected()){
            String s = mWifiManager.getConnectionInfo().getSSID();
            // TODO 不合理,需要改进
            if(s.charAt(0) == '\"' && s.charAt(s.length() - 1) == '\"')
                s = s.substring(1, s.length() - 1);
            if(ssid.equals(s))
                return true;
        }
        return false;
    }


    public static WifiUtil getInstance(){
        if(mWifiUtil == null)
            mWifiUtil = new WifiUtil(AppContext.getContext());
        return mWifiUtil;
    }

    public void openWifiSetting(Context context){
        if(Build.VERSION.SDK_INT > 10){
            context.startActivity(new Intent(Settings.ACTION_SETTINGS));
        }else{
            context.startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
        }
    }

    public boolean isWifiOpen(){
        return this.mWifiManager.isWifiEnabled();
    }

    /**
     *
     * @return if not get the ip return null
     */
    public String getIpAddress(){
        DhcpInfo di = mWifiManager.getDhcpInfo();
        if(di == null)
            return null;
        int intIp = di.ipAddress;
        if(intIp == 0)
            return null;
        return  int2Ip(intIp);
    }

    /**
     *
     * @return if the broadcast
     */
    public String getBroadcastIp(){
        DhcpInfo di = mWifiManager.getDhcpInfo();
        if(di == null)
            return null;
        int intIp = di.ipAddress;
        int mask = di.netmask;
        Message msg = Message.obtain();
        msg.what = HandlerCenter.Flag.BASE_ACTIVITY;

        if(intIp == 0 || mask == 0)
            return null;
        return int2Ip(intIp | (~mask));
    }

    /**
     *  convert the int ip to String ip
     * @param ipAddress
     * @return
     */
    private String int2Ip(int ipAddress)  {
        return ((ipAddress & 0xff)+"."+(ipAddress>>8 & 0xff)+"."
                +(ipAddress>>16 & 0xff)+"."+(ipAddress>>24 & 0xff));
    }


}
